/* Papastefanos Serafeim */
/* Sunarthseis gia asfalh epikoinwnia me SSL */

#ifndef _SECURE_H
#define _SECURE_H

///////////////////////////////////////
////////////// INCLUDES ///////////////
///////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#ifdef _CWMP_WIN
////Windows version includes ////
#include <windows.h>
#include <winsock.h>

#else
//// Unix version include ////
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include <unistd.h>
#include <signal.h>

#endif

#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <openssl/x509v3.h>

#include "helpers.h"
#include "client.h"
#include "custom.h"

///////////////////////////////////////
/////////////// DEFINES ///////////////
///////////////////////////////////////
#ifndef _CWMP_WIN // To UNIX den exei defined ton tupo SOCKET
#define SOCKET unsigned int 
#endif

#define MAXBUF 256 /* H sta9era MAXBUF exei to megisto ari9mo xarakthrwn
pou epitrepetai na periexei mia grammh sthn epikefalida tou HTTP mhnumatos.
Mporoume na ths dwsoume opoia timh 9eloume. */

#define int_error(msg)  handle_error(__FILE__, __LINE__, msg)

#define CIPHER_LIST "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"


#define CAFILE "trustcert.pem" // Auto edw einai to pistopoihtiko pou empistevetai o client mas !
// Sunh9ws 9a prepei na einai to pistopoihtiko ths anwterhs arxhs pou ekdidei ta pistopoihtika
// stous ACS

#define CADIR NULL // Autos o katalogos periexei pistopoihtika pou empistevetai o client mas.
// Ta onomata twn pistopoihtikwn entos tou katalogou prepei na exoun proka9orismenh apo to 
// openssl morfh. 


///////////////////////////////////////
////////// FUNCTION HEADERS ///////////
///////////////////////////////////////

/* Xeirismos la9ous sto OpenSSL */
void handle_error(const char *file, int lineno, const char *msg);

/* Arxikopoihsh ths OpensSSL */
void init_OpenSSL(void);

/* Sunarthsh gia to verify twn pistopoihtikwn */
int verify_callback(int ok, X509_STORE_CTX *store);

/* Sunarthsh gia elegxo meta th sundesh */
long post_connection_check(SSL *ssl, char *host);

/* Sunarthsh gia seeding tou random number generator */
void seed_prng(void);

/* Sunarthsh gia setup tou CTX tou client */
SSL_CTX *setup_client_ctx(void);

/* Stellei ta StringList ws HTTP mhnuma - ka9e StringList einai kai ena
oloklhro soap message - ekdosh SSL*/
void sendRawMessageSSL(SSL *, StringList *);

/* Diavazei ena mhnuma pou elave - isws uparxoun perissotera apo ena mhnumata 
   - ekdosh SSL*/
char *getRawMessageSSL(SSL *);


/* Xrhsimopoiei tis treis parapanw sunarthseis gia na epistrefei ap'eu9eias ta 
mhnumata pou esteile o server - ekdosh SSL */
StringList *getMessagesSSL(SSL *);

/* Diavazei mia grammh - xrhsimopoieitai gia tis epikefalides tou http - ekdosh SSL*/
char *getLineSSL(SSL *);

/* Aparaithto gia to winsock */
void initWinsock(void); 

/* Sundesh sth d/sh kai th 9ura pou perniountai ws parametroi. Xrhsimo gia th
   leitourgia tou client - ekdosh SSL*/
SSL *makeConnectionSSL(char *address, int port, SSL_CTX *ctx);


#endif 
